package com.winter.service;

import com.google.common.base.Strings;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.winter.mapper.FinFavInfoMapper;
import com.winter.model.FinFavInfo;
import com.winter.util.Result;
import com.winter.util.ResultCode;

import java.util.Date;

@Service
public class FinFavInfoService {

    @Autowired
    public FinFavInfoMapper finFavInfoMapper;

    /**
     * 添加报价收藏信息
     */
    public Result insertFinFavInfo(FinFavInfo fin) {
        if (Strings.isNullOrEmpty(fin.getCreateUser())) {
            return Result.failure(ResultCode.USER_NOT_LOGGED_IN);
        }

        Long aLong = finFavInfoMapper.get(fin.getRelaId(), fin.getRelaType(), Integer.valueOf(fin.getRelaChildType()), fin.getCreateUser());
        if (aLong > 0) {
            return Result.failure(ResultCode.PARAM_IS_BLANK, "你已经收藏过啦");
        }
        fin.setCreatetime(new Date());
        fin.setUpdatetime(new Date());
        fin.setUpdateUser(fin.getCreateUser());
        int i = finFavInfoMapper.insertSelective(fin);
        if (i > 0) {
            return Result.success("收藏成功");
        } else {
            return Result.failure(ResultCode.INSERT_ERROR);
        }
    }

    public Result deleteFinFavInfo(String relaId, String relaType, String relaChildType, String userNo) {
        finFavInfoMapper.delete(relaId, relaType, relaChildType, userNo);
        return Result.success("取消收藏成功");
    }
}
